package bsc.war.form;

import bsc.ejb.metadata.Framework;
import bsc.ejb.metadata.MapDrawing;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.FileUtils;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.apache.commons.lang.math.NumberUtils;

/**
 *
 * @author luisZavaleta
 */
public class NewMapDrawing extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {


        try {
            Map<String, Object> parameterValues = FileUtils.getMultipartParameters(request);
            byte[] mapFile = (byte[]) parameterValues.get("file");
            boolean edit = false;
            Long mapDrawingId = -1L;

            if (NumberUtils.isDigits((String) parameterValues.get("id")) && ((Long.valueOf((String) parameterValues.get("id"))) > 0)) {
                Long id = Long.valueOf((String) parameterValues.get("id"));
                if (id >= 0) {
                    edit = true;
                    mapDrawingId = id;
                }
            }

            HttpSession session = request.getSession(true);
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            Framework framework = delegate.findFramework((Long)session.getAttribute("frameworkId"));
            MapDrawing mapDrawing = edit ? delegate.findMapDrawing(mapDrawingId) : (MapDrawing) Factory.getBean("MapDrawing");

            // get filename parameter from the html form (imagine you got a input hidden with this data)
            String fileName = (String) parameterValues.get("file_filename");//FileUtils.getParameter(request, "filename");
            if (!edit) delegate.createMapDrawing(mapDrawing);

            if (FileUtils.isImage(fileName)) {
                String mapFileName = FileUtils.generateFilename(mapDrawing, FileUtils.FileType.MAP);
                String mapDrawingFileURL = FileUtils.generateFileURL(mapFileName, FileUtils.FileType.MAP);
                boolean fileSaved = FileUtils.saveFile(mapFile, mapFileName, FileUtils.FileType.MAP);
                if (!fileSaved && !edit) {
                    delegate.deleteMapDrawing(mapDrawing);
                    System.out.println("Borre el mapa agregado");
                } else{
                    mapDrawing.setFile(mapDrawingFileURL);
                    mapDrawing.setFramework(framework);
                    mapDrawing.setName((String) parameterValues.get("name"));//FileUtils.getParameter(request, "name"));
                    mapDrawing.setDescription((String) parameterValues.get("description")); //FileUtils.getParameter(request, "description"));

                    delegate.updateMapDrawing(mapDrawing);//.createMapDrawing(md);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return new ModelAndView("close");
    }
}
/*



 */
